package com.wffz.controller;

import com.wffz.comm.api.ResponseResult;
import com.wffz.po.ClientUser;
import com.wffz.po.SignIn;
import com.wffz.service.SignInService;
import com.wffz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;

/**
 * @author zgy
 * @className SignInController
 * @Description
 * @date 2023-03-15 16:30
 **/
@Slf4j
@RestController
@RequestMapping("/signIn")
public class SignInController {

    @Resource
    private UserService userService;

    @Resource
    private SignInService signInService;

    /**
     * 查询用户签到记录列表
     * @param request
     * @return
     */
    @GetMapping("/queryUserSignInList")
    public ResponseResult queryUserSignInList(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        List<SignIn> list = signInService.queryUserSignInList(clientUser);
        return ResponseResult.success(list);
    }

    /**
     * 用户签到
     * @param request
     * @return
     */
    @GetMapping("/userSignIn")
    public ResponseResult userSignIn(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        int i = signInService.userSignIn(clientUser);
        if (i > 0){
            ResponseResult success =  ResponseResult.success(i);
            success.setMessage("Sign in successfully");
            return success;
        }else{
            ResponseResult error = ResponseResult.error();
            error.setMessage("Failed to sign in");
            return error;
        }
    }

    /**
     * 判断用户是否已签到
     * @param request
     * @return
     */
    @GetMapping("/isExitSignIn")
    public ResponseResult isExitSignIn(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        ClientUser clientUser = userService.findUserByUserName(principal.getName());
        boolean isExistSignIn = signInService.isExitSignIn(clientUser);
        return ResponseResult.success(isExistSignIn);
    }

}
